﻿using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Xml;
using viBlog.Classes;
using visinia.SmartEngine;

namespace viBlog.Controllers
{
    public class RssFeedController
    {
        #region RSS for Posts and Categories
        public static void GenerateRssForLatestPosts(HttpContext context)
        {
            List<Post> posts = PostController.GetPosts(BlogController.PostsCountToSyndicate);
            context.Response.ContentType = "text/xml";
            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
            SetRssHeader(writer);
            foreach (Post post in posts)
            {
                SetRssItem(writer, post);
            }
            SetRssFooter(writer);
            string s = context.Response.OutputStream.ToString();
            context.Response.End();
        }

        public static void GenerateRssForCategory(HttpContext context, string CategoryName)
        {
            Category category = CategoryController.GetCategory(HttpUtility.UrlDecode(CategoryName));
            if (category != null)
            {
                List<Post> posts = PostController.GetPosts(category, BlogController.PostsCountToSyndicate);
                context.Response.ContentType = "text/xml";
                XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
                SetRssHeader(writer);
                foreach (Post post in posts)
                {
                    SetRssItem(writer, post);
                }
                SetRssFooter(writer);
            }
            context.Response.End();
        }        

        private static void SetRssHeader(XmlTextWriter writer)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", BlogController.BlogName);
            writer.WriteElementString("link", WebRoots.GetWebRoot + "Blog.aspx");
            writer.WriteElementString("description", BlogController.BlogTagLine);
            //writer.WriteElementString("copyright", "Copyright 2010 Tanvon Malik www.visinia.com. All rights reserved.");
        }

        private static void SetRssItem(XmlTextWriter writer, Post post)
        {
            writer.WriteStartElement("item");
            writer.WriteElementString("title", post.Title);
            writer.WriteElementString("author", post.Author);
            writer.WriteElementString("description", post.Summary + PostController.GetPostDescription(post.PostId));
            writer.WriteElementString("link", post.Url);
            writer.WriteElementString("pubDate", post.Date.ToString("R"));
            List<Category> categories = CategoryController.GetCategories(post.PostId);
            foreach (Category category in categories)
            {
                writer.WriteElementString("category", category.Name);
            }
            writer.WriteEndElement();
        }

        private static void SetRssFooter(XmlTextWriter writer)
        {
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }         
        #endregion

        #region RSS for latest comments
        public static void GenerateRssForLatestComments(HttpContext context)
        {
            List<Comment> comments = CommentController.GetRecentComments(BlogController.PostsCountToSyndicate);
            context.Response.ContentType = "text/xml";
            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8);
            SetRssHeaderForComments(writer);
            foreach (Comment comment in comments)
            {
                SetRssItemForComments(writer, comment);
            }
            SetRssFooterForComments(writer);
            string s = context.Response.OutputStream.ToString();
            context.Response.End();
        } 
        private static void SetRssHeaderForComments(XmlTextWriter writer)
        {
            writer.WriteStartDocument();
            writer.WriteStartElement("rss");
            writer.WriteAttributeString("version", "2.0");
            writer.WriteStartElement("channel");
            writer.WriteElementString("title", BlogController.BlogName + " - Comments");
            writer.WriteElementString("link", WebRoots.GetWebRoot + "Blog.aspx");
            writer.WriteElementString("description", BlogController.BlogTagLine);
            //writer.WriteElementString("copyright", "Copyright 2010 Tanvon Malik www.visinia.com. All rights reserved.");
        }

        private static void SetRssItemForComments(XmlTextWriter writer, Comment comment)
        {
            Post post = PostController.GetPost(comment.PostId);
            writer.WriteStartElement("item");
            writer.WriteElementString("title", comment.Author + " on " + post.Title);
            writer.WriteElementString("author", comment.Author);
            writer.WriteElementString("description", comment.Content);
            writer.WriteElementString("link", comment.Url);
            writer.WriteElementString("pubDate", comment.Date.ToString("R"));            
            writer.WriteEndElement();
        }

        private static void SetRssFooterForComments(XmlTextWriter writer)
        {
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }         
        #endregion
    }
}
